# delimit ;
clear ;
cd "replication" ;
set more off ; 

* *************************************************************************** ;
* load data 
* *************************************************************************** ;

use "./data/endline.dta", clear ; 

* *************************************************************************** ;
* merge with baseline to get completed_elem (used later for hetero analysis)
* *************************************************************************** ;

merge 1:1 Id using "./data/baseline.dta", keepusing(completed_elem) ;
assert _merge != 1 ; 

* _merge == 2 are respondents with baseline but no endline ; 
drop if _merge == 2 ;
drop _merge ;

* *************************************************************************** ;
* construct outcome variable for total # of behaviors adopted 
* *************************************************************************** ;

* generate local macros to keep track of outcomes per module ;
global module1_behav 	"water_boil_filter water_tap wash_soap3" ;
global module23_behav 	"pregnancy_anc pregnancy_hospital" ;
global module4_behav 	"aids_tested_self condom_use"  ;
global module5_behav 	"nightblindness_food" ;

* generate a variable that takes the sum of all outocomes that the repsondent does ; 
egen behav_all = rowtotal($module1_behav $module23_behav $module4_behav $module5_behav) ; 

* logical check: it should be the same as adding the individual variables ; 
gen behav_all2 = water_boil_filter + water_tap + wash_soap3 + pregnancy_anc + pregnancy_hospital + aids_tested_self + condom_use + nightblindness_food  ;
assert behav_all == behav_all2 ;
drop behav_all2 ;

****************************************************************************** ;
* generate randomization strata and wave-class variable
****************************************************************************** ;

egen strata = group(wave neighborhood female mfi) ;
egen waveclass = group(wave class) ;

* **************************************************************************** ;
* label variables for the LaTeX tables
* **************************************************************************** ;

label var behav_all "\shortstack[l]{Total\\no. of\\behaviors\\adopted}" ;
label var water_boil_filter "\shortstack[l]{Drinks\\filtered\\or\\boiled\\water}" ;
label var water_tap "\shortstack[l]{Uses\\a water\\vessel\\with tap}" ;
label var wash_soap3 "\shortstack[l]{Washes\\hands\\with\\soap\\at least\\thrice\\daily}" ;
label var pregnancy_anc "\shortstack[l]{Would\\go for\\ante-natal\\care if\\self/spouse\\pregnant}" ;
label var pregnancy_hospital "\shortstack[l]{Would\\go for\\hospital\\delivery if\\self/spouse\\pregnant}" ; 
label var aids_tested_self "\shortstack[l]{Has been\\tested\\for HIV\\in the\\last 3\\months}" ; 
label var condom_use "\shortstack[l]{Uses\\condom\\during\\inter-\\course}" ; 
label var nightblindness_food "\shortstack[l]{Eats more\\nutritious\\foods to\\prevent\\night-\\blindness}" ;

label var healthonly "HEE" ; 
label var healthandpay "HEEC" ; 

****************************************************************************** ;
* regressions
****************************************************************************** ;

capture est drop behav_all water* wash* pregnancy* aids* condom* night* ;

foreach var in behav_all $module1_behav $module23_behav $module4_behav $module5_behav  { ;
	
	areg `var' healthonly healthandpay, a(strata) robust ;
	qui est sto `var'; 
	
	test healthonly = healthandpay ; 
	estadd local titlerow = "" ; 
	estadd scalar fstat = r(F): `var' ; 
	estadd scalar pval = r(p): `var' ; 

	summarize `var' if control == 1 & e(sample) == 1;
	estadd scalar cmean = r(mean): `var' ; 
	estadd scalar csd = r(sd): `var' ;

} ;

esttab behav_all water* wash* pregnancy* aids* condom* night* using "./output/table-endline-behavior.tex",
	replace
	drop(_cons) 
	cells(b(label() star fmt(%9.3f %9.3f)) se(par)) 
	star(* 0.10 ** 0.05 *** 0.01) 
	stats(titlerow fstat pval cmean csd N, fmt(%9s %9.3f %9.3f %9.3f %9.3f %9.0f) labels("\(F\)-test, HEE = HEEC" "$\qquad$ \(F\)-statistic" "$\qquad$ \(p\)-value" "Control Mean" "Control SD" "N"))
	prehead(\begin{table}[htbp] \centering \normalsize `"\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}"'
		\captionsetup{justification=centering} 
		\caption{Effects on Health Behaviors}
		\label{table-endline-behavior}
		\begin{tabular*}{\hsize}{p{4cm}p{1.4cm}p{1.4cm}p{1.4cm}p{1.4cm}p{1.4cm}p{1.4cm}p{1.4cm}p{1.4cm}p{1.4cm}}
		\toprule)
	legend label  booktabs  collabels( , none)
	mgroups(
	"All Topics" 
	"\shortstack[c]{Cleanliness\\and Hygiene}"
	"\shortstack[c]{Midwives,\\Maternal \& Child\\Health}"
	"\shortstack[c]{Condoms, AIDS,\\Syphilis}" 
	"\shortstack[c]{Night-\\blindness}"
	, pattern(1 1 0 0 1 0 1 0 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
	postfoot(`"\bottomrule"'  \end{tabular*} \captionsetup{justification=justified, width=\hsize} 
		\caption*{\footnotesize \textit{Notes:} 
		Outcomes are from an endline survey conducted approximately 10 months after the final film screening.
		In Column 1, the dependent variable is the total number of all behaviors that the respondent adopted (up to 8).		
		The dependent variables in Column 2 onwards are dummies for whether the respondent adopted the given behavior. 
		\textit{HEE} is a dummy for assignment to only health entertainment-education. 
		\textit{HEEC} is a dummy for assignment to health entertainment-education with cash incentives for test performance. 
		Robust standard errors.
		All regressions include dummies for randomization strata, where strata are defined by gender, neighborhood, and microfinance client status.
		***\$\,p < 0.01$, **\$\,p < 0.05$, *\$\,p<0.10$.
		}
		\end{table}) ;
		
exit ;

